package com.starter.jienlee.common;


import com.starter.jienlee.common.consts.SysConstant;
import lombok.Data;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * 通用返回类 成功的code 为 0;
 *
 */
@Data
public class RestResult<T> {

    private Integer code;

    private String message;

    private T data;

    public RestResult() {

    }

    public RestResult(Integer code) {
        this.code = code;
    }

    public RestResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public RestResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public static RestResult<Object> success() {
        return new RestResult<>(NumberUtils.INTEGER_ZERO);
    }

    public static <T> RestResult<T> success(T data) {
        return new RestResult<>(NumberUtils.INTEGER_ZERO, data);
    }

    public static RestResult<Object> busy() {
        return new RestResult<>(SysConstant.SYSTEM_BUSY_CODE, SysConstant.SYSTEM_BUSY_CODE_MSG);
    }

    public static RestResult<Object> validationError() {
        RestResult<Object> result = new RestResult<>();
        result.setCode(SysConstant.PARAMETER_ERROR_CODE);
        result.setMessage(SysConstant.PARAMETER_ERROR_MSG);
        return result;
    }

    /**
     * 没有权限
     *
     * @return
     */
    public static RestResult<Object> noAuthPermission() {
        RestResult<Object> result = new RestResult<>();
        result.setCode(SysConstant.NO_AUTH_PERMISSION);
        result.setMessage(SysConstant.PARAMETER_ERROR_MSG);
        return result;
    }

    public static RestResult<Object> failed(Integer code, String message) {
        RestResult<Object> result = new RestResult<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static RestResult<Object> failed(IBaseResultCode resultCode) {
        RestResult<Object> result = new RestResult<>();
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }
}
